package com.changanw.business.service;

import com.changanw.common.feign.OrderServiceApi;
import com.changanw.common.feign.StorageServiceApi;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**

 */
@Service
public class BusinessService {
    @Autowired
    OrderServiceApi orderServiceApi;
    @Autowired
    StorageServiceApi storageServiceApi;

    @GlobalTransactional //分布式事务注解
    public void purchase(String account, String productId, Integer count) {
        //获取全局事务的xid
        String xid = RootContext.getXID();
        BusinessActionContext context = new BusinessActionContext();
        context.setXid(xid);
        //调用订单微服务
        orderServiceApi.prepare(context,account, productId, count);
        //调用库存服务
        storageServiceApi.prepare(context,productId, count);
    }
}